﻿using System;
using System.Data.Common;
using System.IO;
using System.Security;
using System.Xml;

using Centido.Core;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that restores the current domain from a backup.
    /// </summary>
    internal class RestoreCommand : Command
    {
        /// <summary>
        ///   The file name parameter.
        /// </summary>
        public const string FileNameParameter = "file";

        /// <summary>
        ///   The file name parameter (short name).
        /// </summary>
        public const string FileNameShortParameter = "f";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "restore"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.RestoreCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.RestoreCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            if (context.CurrentDomain == null) return;

            var domain = context.CurrentDomain;

            // If there is no permission to perform this operation
            if (!domain.CurrentUser.CanChangeSecurity
                || !domain.CurrentUser.CanChangeSchema)
            {
                Console.WriteLine(Messages.NeedCanChangeSecurityAndSchemaPermissionMessage);
                return;
            }

            var restoreTask = new RestoreTask(domain);

            // If the file name parameter was specified
            if (context.ParameterValueIsNotEmpty(FileNameParameter, FileNameShortParameter))
            {
                // The file name
                var fileName = context.GetParameterValue(FileNameParameter, FileNameShortParameter);

                restoreTask.FileName = fileName;
            }

            // Displaying diagnostic messages
            restoreTask.MessageReceived += (o, e) =>
                Console.WriteLine(e.Message);

            try
            {
                restoreTask.Run();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }
            catch (XmlException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }
            catch (SecurityException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
